<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * Class Feedback
 * @package app\api\controller
 * 常見問題
 */

class Feedback extends Api
{

    /*
     * type 1：用户端 2：司机端
     * 客服中心标题接口
     */
    public function getProblem(){
        if(empty(input('type'))){
            $this->ajaxError('非法输入','',400);
        }
        $result = db('problem')->where('type',input('type'))->field('id,title')->select();
        if(empty($result)){
            $this->ajaxError('无数据','',400);
        }
        $this->ajaxSuccess($result);
    }
    
    /*
     * type 1：用户端 2：司机端
     * 通关id获取问题内容接口
     */
    public function getProblemContentById(){
        if(empty(input('id'))){
            $this->ajaxError('请传入ID','',400);
        }
        $result = db('problem')->where('id',input('id'))->field('id,problem_title,problem_content')->find();
        if(empty($result)){
            $this->ajaxError('该标题无问题','',400);
        }
        $this->ajaxSuccess($result);
    }
    
    /*
     * 
     * 接收用户反馈接口
     */
    public function receiveFeekback(){
        if(empty(input('feekback')) || empty(input('id'))){
            $this->ajaxError('非法输入','',400);
        }
        $data['uid'] = $this->uid;
        $data['cid'] = input('id');
        $data['feekback'] = input('feekback');
        $data['feekback_time'] = time();
        $result = db('feekback')->insert($data);
        if(empty($result)){
            $this->ajaxError('操作失败！','',400);
        }
        $this->ajaxSuccess();
    }
    
    /**
     * 
     * 历史反馈接口
     */
    public function historyFeekback(){
        $result = db('feekback')
        ->alias('a')
        ->join('problem b','a.cid = b.id')
        ->where('a.uid',$this->uid)
        ->field("a.*,FROM_UNIXTIME(feekback_time,'%Y-%m-%d %H:%i:%s') as feekback_time,FROM_UNIXTIME(reply_time,'%Y-%m-%d %H:%i:%s') as reply_time,b.problem_title")
        ->select();
        if(empty($result)){
            $this->ajaxError('暂无反馈信息','',400);
        }
        $this->ajaxSuccess($result);
    }
    
}
